var WzAdvInfoDlg = {
    title:'',
    subtype:'',
    type:'',
    userCode:'',
    coverUrl:'',
    advDetail:'',
    hasDetail:'',
    couponIds: '',
    advQuestionnaireId: '',
    advQuestionId: '',
    begin:'',
    end:'',
    totalReward:'',
    pushNumber:'',
    rewardType:'',
    rewardAverage:'',
    rewardRange:'',
    expense:'',
    screenGender:'',
    userInfoId:'',
    screenCity:'',
    wzAdvInfoData : {}
};

/**
 * 关闭此对话框
 */
WzAdvInfoDlg.close = function() {
    parent.layer.close(window.parent.WzAdv.layerIndex);
}
$(document).ready(function () {
    $("#wizard").steps();
    $("#form").steps({
        bodyTag: "fieldset",
        onStepChanging: function (event, currentIndex, newIndex) {
            // Always allow going backward even if the current step contains invalid fields!
            if (currentIndex > newIndex) {
                return true;
            }
            var form = $(this);
            // Clean up if user went backward before
            if (currentIndex < newIndex) {
                // To remove error styles
                $(".body:eq(" + newIndex + ") label.error", form).remove();
                $(".body:eq(" + newIndex + ") .error", form).removeClass("error");
            }
            if (currentIndex === 0) {
                var selected = $('#WzUserTable').bootstrapTable('getSelections');
                if (selected[0] != null) {
                    localStorage.removeItem("userCode");
                    var userCode=selected[0]['userCode'];
                    localStorage.setItem("userCode",userCode);
                    WzAdvInfoDlg.userCode=userCode;

                    var queryData = {};
                    queryData['userCode'] = userCode;
                    WzUserInfo.table.refresh({query: queryData});
                }else{
                    Feng.error('商户必选!');
                    return false;
                }
                WzAdvInfoDlg.title=$("#title").val();
                WzAdvInfoDlg.subtype=$("#subtype").val();
                WzAdvInfoDlg.type=$("#type").val();
            }
            if (currentIndex === 1) {
                var selected = $('#WzPicFileTable').bootstrapTable('getSelections');
                if (selected[0] != null) {
                    var coverUrl=selected[0]['fileUrl'].substring(26);
                    WzAdvInfoDlg.coverUrl=coverUrl;
                }else{
                    Feng.error('封面图必选!');
                    return false;
                }
            }
            if (currentIndex === 2) {
                var details=$("#details").val();
                if(details.length==0){
                    // Feng.error('请至广告编辑器编辑内容后再进行下一步');
                    // return false;
                    WzAdvInfoDlg.hasDetail='0';
                }else{
                    WzAdvInfoDlg.advDetail=details;
                    WzAdvInfoDlg.hasDetail='1';
                }
            }
            // if (currentIndex === 3) {
            //     var screenGender=$("#screenGender").val();
            //     WzAdvInfoDlg.screenGender=screenGender;
            //     var ajax = new $ax(Feng.ctxPath + "/wzAdv/getInfo", function (data) {
            //     }, function (data) {
            //     });
            //     ajax.set("screenGender", WzAdvInfoDlg.screenGender);
            //     ajax.start()
            //
            // }
            if (currentIndex === 3) {
                var selected = $('#WzCouponTable').bootstrapTable('getSelections');
                // Feng.alert("提交优惠券测试")
                if (selected.length > 0) {
                    var couponIds = "";
                    for (var i = 0; i < selected.length; i++) {
                        couponIds = couponIds + selected[i]['couponId'] + ",";
                    }
                    WzAdvInfoDlg.couponIds=couponIds;
                }
            }
            if (currentIndex === 4) {
                var selected = $('#WzQuestionnaireTable').bootstrapTable('getSelections');
                // Feng.alert("提交商家问卷测试")
                if (selected[0] != null) {
                    // console.log(selected[0]['questionnaireId']);
                    var advQuestionnaireId = selected[0]['questionnaireId'];
                    WzAdvInfoDlg.advQuestionnaireId=advQuestionnaireId;
                }
            }
            if (currentIndex === 5) {
                var selected = $('#WzQuestionTable').bootstrapTable('getSelections');
                // Feng.alert("提交广告问题测试")
                if (selected[0] != null) {
                    var advQuestionId = selected[0]['questionnaireId'];
                    WzAdvInfoDlg.advQuestionId=advQuestionId;
                }
            }
            if (currentIndex === 6) {
                var selected = $('#WzUserInfoTable').bootstrapTable('getSelections');
                // Feng.alert("提交广告问题测试")
                if (selected[0] != null) {
                    var userInfoId = selected[0]['userInfoId'];
                    WzAdvInfoDlg.userInfoId=userInfoId;
                }
            }
            // Disable validation on fields that are disabled or hidden.
            form.validate().settings.ignore = ":disabled,:hidden";

            // Start validation; Prevent going forward if false
            return form.valid();
        },
        onStepChanged: function (event, currentIndex, priorIndex) {
            // Suppress (skip) "Warning" step if the user is old enough.
            // if (currentIndex === 2 && Number($("#age").val()) >= 18) {
            //     $(this).steps("next");
            // }
            // Suppress (skip) "Warning" step if the user is old enough and wants to the previous step.
            //if (currentIndex === 2 && priorIndex === 3) {
            //    $(this).steps("previous");
            //}
        },
        onFinishing: function (event, currentIndex) {
            var form = $(this);
            // Disable validation on fields that are disabled.
            // At this point it's recommended to do an overall check (mean ignoring only disabled fields)
            form.validate().settings.ignore = ":disabled";
            // Start validation; Prevent form submission if false
            return form.valid();
        },
        onFinished: function (event, currentIndex) {
            var beginTime=$("#beginTime").val();
            var endTime=$("#endTime").val();
            WzAdvInfoDlg.begin=beginTime;
            WzAdvInfoDlg.end=endTime;
            if(beginTime!=null&&endTime!=null&&(beginTime>endTime || beginTime==endTime)){
                Feng.error("添加失败！开始时间不能晚于等于结束时间！");
                $("#beginTime").css("background", "pink");
                $("#beginTime").focus();
                return;
            }
            WzAdvInfoDlg.totalReward=$("#totalReward").val();
            WzAdvInfoDlg.pushNumber=$("#pushNumber").val();
            WzAdvInfoDlg.rewardType=$("#rewardType").val();
            WzAdvInfoDlg.rewardAverage=$("#rewardAverage").val();
            WzAdvInfoDlg.rewardRange=$("#rewardRange").val();
            WzAdvInfoDlg.expense=$("#expense").val();
            WzAdvInfoDlg.screenGender=$("#screenGender").val();
            WzAdvInfoDlg.screenCity=$("#screenCity").val();
            // 提交信息
            var ajax = new $ax(Feng.ctxPath + "/wzAdv/add", function(data){
                if(data.code == 100){
                    Feng.error(data.message);
                }else if(data.code == 200){
                    Feng.success("添加成功!");
                    window.parent.WzAdv.table.refresh();
                    WzAdvInfoDlg.close();
                }
            },function(data){
                Feng.error("添加失败!" + data.responseJSON.message + "!");
            });
            ajax.set(WzAdvInfoDlg);
            ajax.start();

            // var form = $(this);
            // form.submit();
            // Feng.success("添加成功，请刷新表格");
            // window.parent.WzAdv.table.refresh();
            // WzAdvInfoDlg.close();

        },
        onCanceled: function () {
            parent.layer.close(window.parent.WzAdv.layerIndex);
        }
    }).validate({
        errorPlacement: function (error, element) {
            element.before(error);
        },
        rules: {
            totalReward:"isPositiveInt",
            pushNumber:"isPositiveInt",
            rewardAverage:"isPositiveInt",
            rewardRange:"isPositiveInt",
            expense:"isPositiveInt"
        },
        messages: {

        }
    });
    jQuery.validator.addMethod("isPositiveInt", function(value, element) {
        var tel = /^[0-9]+$/;
        return this.optional(element) || (tel.test(value));
    }, "请输入正确的数字");
});

// var saveAdvDetail = function () {
//     $("#eg").removeClass("no-padding");
//     var aHTML = $('.click2edit').code();//save HTML If you need(aHTML: array).
//     console.log(aHTML);
//     $('.click2edit').destroy();
// };
